cmake_minimum_required(VERSION 3.1)
cmake_policy(SET CMP0048 NEW)
project(tzsptap2pcap VERSION 0.1.0.0)

SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wno-unused-function")

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

include(ExternalProject)

# 定义 zlog 项目的编译过程
ExternalProject_Add(
    zlog_project
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libs/zlog-1.2.15  # 源码目录
    CONFIGURE_COMMAND ""  # 不执行配置命令
    BUILD_COMMAND make  # 执行 make 命令进行编译
    BUILD_IN_SOURCE 1  # 在源码目录中进行编译
    INSTALL_COMMAND ""  # 不执行安装命令
    BUILD_BYPRODUCTS ${CMAKE_CURRENT_SOURCE_DIR}/libs/zlog-1.2.15/src/libzlog.a
    COMMENT "Compiling zlog library"
)

# 获取 zlog 库的路径
set(ZLOG_LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libs/zlog-1.2.15)
set(ZLOG_LIB_NAME zlog)
set(ZLOG_LIB ${ZLOG_LIB_DIR}/src/lib${ZLOG_LIB_NAME}.a)
set (EXTRALIB ${EXTRALIB} ${ZLOG_LIB})

# 定义 libev 项目的编译过程
ExternalProject_Add(
    evlib_project
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libs/libev-4.33  # 源码目录
    CONFIGURE_COMMAND ./configure --enable-shared=no --enable-static=yes  # 执行配置命令
    BUILD_COMMAND make  # 执行 make 命令进行编译
    BUILD_IN_SOURCE 1  # 在源码目录中进行编译
    INSTALL_COMMAND ""  # 不执行安装命令
    BUILD_BYPRODUCTS ${CMAKE_CURRENT_SOURCE_DIR}/libs/ibev-4.33/.libs/libev.a
    COMMENT "Compiling libev 4.33 library"
)

# 获取 libev 库的路径
set(EV_LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libs/libev-4.33)
set(EV_LIB_NAME ev)
set(EV_LIB ${EV_LIB_DIR}/.libs/lib${EV_LIB_NAME}.a)
set (EXTRALIB ${EXTRALIB} ${EV_LIB})

# options
option(DEBUG "enable debug" ON)

# include aux cmake scripts
include("cmake/Gengetopt.cmake")
ADD_GENGETOPT_FILES(CMDLINE cmdline.ggo)

include("cmake/FindMosquitto.cmake")
set (EXTRALIB ${EXTRALIB} ${MOSQUITTO_LIBRARIES})

#include("cmake/Findlibev.cmake")
#set (EXTRALIB ${EXTRALIB} ${LIBEV_LIBRARY})

find_package(Threads)
set (EXTRALIB ${EXTRALIB} ${CMAKE_THREAD_LIBS_INIT})

set(TZSP_src
arg.c
main.cpp
common.cpp
tzsptap2pcap.cpp
jsoncpp.cpp
)

include_directories("${CMAKE_CURRENT_BINARY_DIR}"
                        "${CMAKE_SOURCE_DIR}/"
                        "${CMAKE_SOURCE_DIR}/include/"
                        "${ZLOG_LIB_DIR}/src/"
                        "${EV_LIB_DIR}/")
# configure a header file to pass some of the CMake settings
# to the source code
configure_file (
	"${PROJECT_SOURCE_DIR}/config.h.in"
	"${PROJECT_BINARY_DIR}/config.h"
	)

# target
add_executable(tzsptap2pcap ${CMDLINE} ${TZSP_src})
add_dependencies(tzsptap2pcap evlib_project)
add_dependencies(tzsptap2pcap zlog_project)
target_link_libraries(tzsptap2pcap ${EXTRALIB} m)
install(TARGETS tzsptap2pcap DESTINATION /usr/local/bin)
install(FILES tzsptap2pcap_log.cfg DESTINATION /etc)

#install(TARGETS tzsptap2pcap DESTINATION /usr/local/bin)
#install(PROGRAMS files/tzsptap2pcap.sh DESTINATION /usr/local/sbin)
#install(FILES files/tzsptap2pcap.ini DESTINATION /etc)
#install(FILES files/p_capture DESTINATION /etc/init.d/ PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
# 自动设置 systemctl enable
#install(CODE "
#    execute_process(
#        COMMAND systemctl daemon-reload
#        RESULT_VARIABLE DAEMON_RELOAD_RESULT
#        OUTPUT_VARIABLE DAEMON_RELOAD_OUTPUT
#        ERROR_VARIABLE DAEMON_RELOAD_ERROR
#    )
#    if(NOT DAEMON_RELOAD_RESULT EQUAL 0)
#        message(FATAL_ERROR \"Failed to reload systemd manager configuration: ${DAEMON_RELOAD_ERROR}\")
#    endif()
#
#    execute_process(
#        COMMAND systemctl enable p_capture
#        RESULT_VARIABLE ENABLE_RESULT
#        OUTPUT_VARIABLE ENABLE_OUTPUT
#        ERROR_VARIABLE ENABLE_ERROR
#    )
#    if(NOT ENABLE_RESULT EQUAL 0)
#        message(FATAL_ERROR \"Failed to enable service: ${ENABLE_ERROR}\")
#    endif()
#")
